package com.lplb.api.admin.service.impl.platform;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lplb.api.admin.mapper.AdminAuthMapper;
import com.lplb.api.admin.model.dto.AdminAuthDTO;
import com.lplb.api.admin.model.entity.AdminAuth;
import com.lplb.api.admin.model.vo.AdminAuthVO;
import com.lplb.api.admin.service.platform.IAdminAuthService;
import com.lplb.common.core.result.R;
import com.lplb.common.core.service.BaseIServiceImpl;
import com.lplb.common.core.utils.BeanDtoVoUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 资源表 管理端服务接口实现
 * </p>
 *
 * @author ws
 * @date 2020-07-21
 */
@RestController
public class AdminAuthServiceImpl extends BaseIServiceImpl<AdminAuthMapper, AdminAuth> implements IAdminAuthService {


    /**
     *  分页查询
     */
    @Override
    public R<IPage<AdminAuthVO>> findPage() {
        Page<AdminAuth> page = this.page(this.getPage(), new LambdaQueryWrapper<AdminAuth>()
                .orderByAsc(AdminAuth::getCreateTime)
        );
        return R.success(BeanDtoVoUtil.pageVo(page, AdminAuthVO.class));
    }


    /**
     *  查询所有
     */
    @Override
    public R<List<AdminAuthVO>> findList() {
        List<AdminAuth> list = this.list(new LambdaQueryWrapper<AdminAuth>()
                .orderByAsc(AdminAuth::getCreateTime)
        );
        return R.success(BeanDtoVoUtil.listVo(list, AdminAuthVO.class));
    }


    /**
     *  查询所有--树结构
     * @return
     */
    @Override
    public R<List<AdminAuthVO>> findTree() {
        List<AdminAuth> adminListAll = this.list(new LambdaQueryWrapper<AdminAuth>().orderByDesc(AdminAuth::getMethod));
        if (adminListAll.size() == 0) {
            return R.success(null);
        }
        // 返回
        List<AdminAuthVO> resultListVO = new ArrayList<>();
        // 根据模块的服务名分组
        Map<String, List<AdminAuth>> adminAuthMap = adminListAll.stream().collect(Collectors.groupingBy(AdminAuth::getServerName));
        adminAuthMap.forEach((k, v) -> {//v=每个服务下的所有接口
            // 拼接服务级别的数据，数据库是不存在的, 数据库只到了类级别
            AdminAuthVO serverAdminAuthVO = new AdminAuthVO();
            serverAdminAuthVO.setId("");
            serverAdminAuthVO.setMethod("");
            serverAdminAuthVO.setUrl("");
            serverAdminAuthVO.setPid("");
            serverAdminAuthVO.setDesc(k);
            serverAdminAuthVO.setServerName(k);
            //服务/模块
            List<AdminAuthVO> adminAuthListVO = new ArrayList<>();
            for (AdminAuth adminAuth : v) {
                if ("".equals(adminAuth.getPid())) {
                    //类上
                    AdminAuthVO adminAuthVO = adminAuth.convert(AdminAuthVO.class);
                    for (AdminAuth adminAuthMethod : v) {
                        if (adminAuth.getId().equals(adminAuthMethod.getPid())) {
                            // 方法上,追加-新增
                            if (adminAuthVO.getAdminAuthVO() != null) {
                                adminAuthVO.getAdminAuthVO().add(adminAuthMethod.convert(AdminAuthVO.class));
                            } else {
                                adminAuthVO.setAdminAuthVO(new ArrayList<AdminAuthVO>() {{
                                    add(adminAuthMethod.convert(AdminAuthVO.class));
                                }});
                            }
                        }
                    }
                    adminAuthListVO.add(adminAuthVO);
                }
            }
            serverAdminAuthVO.setAdminAuthVO(adminAuthListVO);
            resultListVO.add(serverAdminAuthVO);
        });
        return R.success(resultListVO);
    }


    /**
     * ID 查询
     *
     * @return
     */
    @Override
    public R<AdminAuthVO> findId(@RequestParam String id) {
        AdminAuth adminAuth = this.getById(id);
        return R.success(BeanDtoVoUtil.convert(adminAuth, AdminAuthVO.class));
    }


    /**
     * 新增
     */
    @Override
    public R<Void> add(@RequestBody @Validated AdminAuthDTO dto) {
        this.save(dto.convert(AdminAuth.class));
        return R.success();
    }


    /**
     *  编辑
     */
    @Override
    public R<Void> upd(@RequestBody @Validated AdminAuthDTO dto) {
        this.updateById(dto.convert(AdminAuth.class));
        return R.success();
    }


    /**
     *  ID 删除
     */
    @Override
    public R<Void> removeId(@RequestParam String id) {
        this.removeById(id);
        return R.success();
    }

}

